﻿/*************************************************
日志
author：Daixiwei
**************************************************/
using System;
using System.Text;

namespace taurus.client
{
    /**
    * 日志等级
    */
    public enum LoggerLevel
    {
        DEBUG,
        INFO,
        WARN,
        ERROR,
        TRACE
    }

    public static class Logger
    {
        public static Action<LoggerLevel, string> loggerPrint;
        
        private static string threadStack = string.Empty;
        static StringBuilder sb = new StringBuilder(256);

        private static void _print(LoggerLevel level,object str)
        {
            var msg = string.Empty;
            lock (sb)
            {
                sb.Remove(0, sb.Length);
                DateTime time = DateTime.Now;
                sb.Append(time.Hour);
                sb.Append(":");
                sb.Append(time.Minute);
                sb.Append(":");
                sb.Append(time.Millisecond);
                sb.Append("-> ");
                sb.Append(str.ToString());
                msg = sb.ToString();
            }

            if (loggerPrint != null)
            {
                loggerPrint(level, msg);
            }
            else
            {
                Console.WriteLine(str);
            }
        }

        public static void debug(object msg)
        {
            _print(LoggerLevel.DEBUG, msg);
        }

        public static void info(object msg)
        {
            _print(LoggerLevel.INFO, msg);
        }

        public static void warn(object msg)
        {
            _print(LoggerLevel.WARN, msg);
        }

        public static void error(object msg)
        {
            _print(LoggerLevel.ERROR, msg);
        }


        public static void error(Exception e)
        {
            threadStack = e.StackTrace;
            error(e.Message + "\n" + threadStack);
        }

        public static void error(object msg, Exception e)
        {
            threadStack = e.StackTrace;
            string str = msg.ToString() + e.Message;
            error(str + "\n" + threadStack);
        }


    }


}

